package com.ruoyi.web.controller.module.customer;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.customer.service.ICustomerUserService;
import com.ruoyi.customer.domain.CustomerReport;
import com.ruoyi.customer.domain.CustomerUser;
import com.ruoyi.customer.service.ICustomerReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 投诉建议信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/customer/report")
public class CustomerReportController extends BaseController
{
    @Autowired
    private ICustomerReportService customerReportService;

    @Autowired
    private ICustomerUserService customerUserService;

    /**
     * 获取投诉建议列表
     */
    @PostMapping("/customerReportList")
    public TableDataInfo list(@RequestBody CustomerReport customerReport, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            customerReport.setShopId(shopId);
        }
        startPageByPost();
        List<CustomerReport> list = customerReportService.selectCustomerReportList(customerReport);
        return getDataTable(list);
    }

    /**
     * 通过投诉建议ID查询投诉建议
     */
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(id))
        {
            CustomerReport customerReport = customerReportService.selectCustomerReportById(id);
            ajax.put(AjaxResult.DATA_TAG, customerReport);
        }
        return ajax;
    }

    /**
     * 新增投诉建议
     */
    @Log(title = "投诉建议管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping("/customerReportCreate")
    public AjaxResult customerReportCreate(@Validated @RequestBody CustomerReport customerReport, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            customerReport.setShopId(shopId);
        }
        Long userId = SecurityUtils.getUserId();
        CustomerUser cu = customerUserService.selectUserById(userId);
        customerUserService.checkUserAllowed(cu);
        return toAjax(customerReportService.insertCustomerReport(customerReport));
    }

    /**
     * 修改投诉建议
     */
    @PreAuthorize("@ss.hasPermi('customer:report:edit')")
    @Log(title = "投诉建议管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CustomerReport customerReport)
    {
        return toAjax(customerReportService.updateCustomerReport(customerReport));
    }

    /**
     * 删除投诉建议
     */
    @PreAuthorize("@ss.hasPermi('customer:report:remove')")
    @Log(title = "投诉建议管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds)
    {
        return toAjax(customerReportService.deleteCustomerReportByReportIds(reportIds));
    }

}
